<template>
  <md-nav-bar title="我的小店" backUrl="/pages/my/index" backgroundColor="#ffffff" />
  <view class="shop-container">
    <!-- 顶部tab -->
    <view class="shop-tab">
      <view :class="['tab-item', tabIndex === 0 ? 'active' : '']" @click="tabIndex = 0">商品</view>
      <view :class="['tab-item', tabIndex === 1 ? 'active' : '']" @click="tabIndex = 1">订单</view>
    </view>

    <!-- 商品列表 -->
    <view v-if="tabIndex === 0">
      <md-page-list :api="api.Order.goodPage" :params="{ id: data.userId }" @change="pageGoodsChange" ref="goodPageRef">
        <view v-for="(item, idx) in data.goodsList" :key="item.skuId" class="goods-item" @click="onToGoodDetail(item)">
          <view class="goods-index">{{ (idx + 1).toString().padStart(2, '0') }}</view>
          <image class="goods-img" :src="item.imageUrl" mode="aspectFill" />
          <view class="goods-detail">
            <view class="goods-info-container">
              <view class="goods-info">
                <view class="goods-title">{{ item.productName }}</view>
                <view class="goods-type">{{ subType(item.type) }}</view>
                <view class="goods-status">{{ goodsStatus(item.status) }}</view>
              </view>
              <view class="goods-price">￥{{ formatUnitCentToYuan(item.price,2) }}</view>
            </view>
            
            <view class="goods-actions">
              <button class="btn btn-del" v-if="item.status === 0" @click.stop="onDel(item)">删除</button>
              <button class="btn btn-off" v-if="item.status === 1"  @click.stop="onOff(item)">下架修改</button>
              <button class="btn btn-on" v-if="item.status === 0" @click.stop="onOn(item)">上架</button>
            </view>
          </view>
        </view>
      </md-page-list>
    </view>  
    <!-- 订单tab内容可自行补充 -->
    <view v-else>
      <md-page-list :api="api.Order.orderPage" :params="{ sellerId: data.userId }" @change="pageOrderChange" ref="orderPageRef">
        <view
          v-for="(order, idx) in data.orderList"
          :key="idx"
          class="order-card"
          @click="onToDetail(order)"
        >
          <!-- 订单头部 -->
          <view class="order-header">
            <view class="order-id">
              <image
              class="order-id-dot"
              src="https://cdn.xmxiaomai.com/isong_static/my/banwan.png"
              mode="widthFix"></image>
              订单编号：{{ order.orderId }}
            </view>
            <view class="order-status">{{ orderStatus(order.status) }}</view>
          </view>
          <!-- 商品信息 -->
          <view class="order-body">
            <image
              class="order-img"
              :src="order.imageUrl"
              mode="aspectFill"
            />
            <view class="order-info">
              <view class="order-title">{{ order.title }}</view>
              <view class="order-type">{{ order.spuName }}</view>
            </view>
          </view>
          <!-- 订单底部 -->
          <view class="order-footer">
            <view class="order-count">共{{ `1` }}件</view>
            <view class="order-total">
              合计：<text class="order-price">￥{{ formatUnitCentToYuan(order.price, 2) }}</text>
            </view>
          </view>
        </view>
      </md-page-list>
    </view>
    <!-- 底部添加按钮 -->
    <view v-if="tabIndex === 0" class="shop-footer">
      <button class="add-btn" @click="onAdd">添 加</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app';
import { formatUnitCentToYuan, goodsStatus, subType, Toast, orderStatus } from '@/utils/util';

// 接口
import api from '@/api/index';
import type { User } from '@/api/data';

const goodPageRef = ref<any>(null);
const orderPageRef = ref<any>(null);
  
const data = reactive({
  goodsList: [] as User.Order.good[], // 商品列表
  orderList: [] as User.Order.List[], // 订单列表
  userId: ''
});
const tabIndex = ref(0)

// 获取我的商品列表
const pageGoodsChange = (list: User.Order.good[]) => {
  data.goodsList = list;
};

// 获取销售订单列表
const pageOrderChange = (list: User.Order.List[]) => {
  data.orderList = list;
};
// 删除
const onDel = (item: any) => {
  uni.showModal({
    title: '提示',
    content: '是否确认删除该商品？',
    success: async (res) => {
      if (res.confirm) {
        try {
          const res = await api.Order.productDelete({
            spuId: item.spuId,
            skuId: item.skuId,
          });
          if(res.code === 0) {
            Toast('删除成功');
            goodPageRef.value?.onRefresh?.({ id: data.userId});
          }
        } catch (error) {}
      }
    },
  });
}
// 下架
async function onOff(item: any) {
  try {
    const res = await api.Order.productUpdate({
      spuId: item.spuId,
      skuId: item.skuId,
      status: 0
    });
    if(res.code === 0) {
      Toast('下架成功');
      goodPageRef.value?.onRefresh?.({ id: data.userId});
    }
  } catch (error) {}
}
// 上架
async function onOn(item: any) {
  try {
    const res = await api.Order.productUpdate({
      spuId: item.spuId,
      skuId: item.skuId,
      status: 1
    });
    if(res.code === 0) {
      Toast('上架成功');
      goodPageRef.value?.onRefresh?.({ id: data.userId});
    }
  } catch (error) {}
}
// 新增
function onAdd() {
  uni.navigateTo({
    url: '/pages/sub/my/addGoods'
  });
}

// 商品跳详情
const onToGoodDetail = (order) => {
  uni.navigateTo({
    url: '/pages/sub/my/goodsDetail?spuId='+(order.spuId || '')+'&skuId='+(order.skuId || ''),
  });
}

// 订单跳详情
const onToDetail = (order) => {
  uni.navigateTo({
    url: '/pages/sub/my/myOrderDetail?orderId='+(order.orderId || ''),
  });
}

onLoad(() => {
  data.userId = uni.getStorageSync('auth')?.userId;
});
</script>

<style lang="scss" scoped>
.shop-container {
  min-height: 100vh;
  background: #f5f8f7;
  padding-bottom: 120rpx;
}
.shop-tab {
  display: flex;
  background: #fff;
  border-bottom: 2rpx solid #f0f0f0;
}
.tab-item {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  color: #222;
  padding: 32rpx 0 18rpx 0;
  font-weight: 500;
  position: relative;
  opacity: 0.8;
}
.tab-item.active {
  color: #1fa37a;
  opacity: 1;
}
.tab-item.active::after {
  content: '';
  display: block;
  width: 48rpx;
  height: 6rpx;
  background: #1fa37a;
  border-radius: 3rpx;
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
}
.goods-item {
  display: flex;
  background: #fff;
  border-radius: 0;
  border-bottom: 1rpx solid #f0f0f0;
  padding: 28rpx 26rpx 30rpx 20rpx;
  position: relative;
}
.goods-index {
  width: 48rpx;
  text-align: center;
  color: #545353;
  font-size: 32rpx;
  font-weight: bold;
  margin-top: 41rpx;
  margin-right: 24rpx;
}
.goods-img {
  width: 120rpx;
  height: 120rpx;
  border-radius: 20rpx;
  background: #eee;
  margin-right: 24rpx;
  flex-shrink: 0;
}
.goods-detail {
  flex: 1;
}
.goods-info-container {
  display: flex;
  justify-content: space-between;
}
.goods-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
}
.goods-title {
  font-size: 30rpx;
  color: #222;
  font-weight: 500;
  margin-bottom: 8rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.goods-type {
  font-size: 28rpx;
  color: #555;
  margin-bottom: 8rpx;
}
.goods-status {
  font-size: 24rpx;
  color: #545353;
}
.goods-price {
  color: #FF7900;
  font-size: 40rpx;
  font-weight: bold;
  margin-left: 12rpx;
  text-align: right;
  flex-shrink: 0;
  width: 220rpx;

}
.goods-actions {
  display: flex;
  margin-left: 12rpx;
  justify-content: flex-end;
  margin-top: 26rpx;
  text-align: right;
}
.btn {
  font-size: 26rpx;
  border-radius: 32rpx;
  padding: 0 28rpx;
  height: 56rpx;
  line-height: 56rpx;
  margin-left: 12rpx;
  border: 2rpx solid #ff6600;
  background: #fff;
  color: #ff6600;
  margin-right: 0;
}
.btn-del {
  border: 2rpx solid #e0e0e0;
  color: #888;
}
.btn-off {
  border: 2rpx solid #F7320F;
  color: #F7320F;
}
.btn-on {
  border: 2rpx solid #F7320F;
  color: #F7320F;
}
.shop-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 20rpx;
  padding: 24rpx 24rpx 32rpx 24rpx;
  z-index: 10;
}
.add-btn {
  width: 100%;
  height: 90rpx;
  background: #01604A;
  color: #fff;
  font-size: 36rpx;
  border-radius: 60rpx;
  font-weight: 600;
  border: none;
  box-shadow: 0 4rpx 16rpx 0 #e0e0e0;
}
.order-card {
  background: #fff;
  border-radius: 24rpx;
  margin: 24rpx 24rpx 0 24rpx;
  box-shadow: none;
  padding: 0;
}
.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 28rpx 24rpx 0 24rpx;
  font-size: 28rpx;
}
.order-id {
  color: #222;
  display: flex;
  align-items: center;
  font-size: 26rpx;
}
.order-id-dot {
  display: inline-block;
  width: 15rpx;
  height: 30rpx;
  margin-right: 12rpx;
}
.order-status {
  color: #1fa37a;
  font-size: 28rpx;
}
.order-body {
  display: flex;
  align-items: center;
  padding: 18rpx 24rpx 18rpx 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
}
.order-img {
  width: 96rpx;
  height: 96rpx;
  border-radius: 16rpx;
  background: #eee;
  margin-right: 18rpx;
}
.order-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.order-title {
  font-size: 30rpx;
  color: #222;
  font-weight: 500;
  margin-bottom: 8rpx;
  width: 320rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.order-type {
  font-size: 24rpx;
  color: #888;
}
.order-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 18rpx 24rpx 24rpx 24rpx;
  font-size: 28rpx;
}
.order-count {
  color: #888;
  margin-right: 18rpx;
  font-size: 26rpx;
}
.order-total {
  color: #222;
}
.order-price {
  color: #1fa37a;
  font-size: 32rpx;
  font-weight: 600;
  margin-left: 4rpx;
}
</style>